<?php
//=========================
//Vuong Van Binh
//wanbin@vietnambiz.com
//=========================

//ACTION DEL MULTI PROPERTY
function del_multi_favourite_property(){
    global $db;
    $user_id = intval($_SESSION["login"]["id"]);
    if($_POST){
        $arr_id = $_POST["check"];
        foreach($arr_id as $i=>$id){
            $db->exec_delete('favourite_property',"`pid`=".intval($id)." AND uid=".$user_id);
        }
    }
}
function del_multi_property(){

    global $db;

    $user_id = intval($_SESSION["login"]["id"]);

    if($_POST){

        $arr_id = $_POST["check"];
        foreach($arr_id as $i=>$id){

            $db->exec_delete('property',"`id`=".intval($id)." AND member_id=".$user_id);

        }
    }

}
function get_property_contact($id) {

    global $db;
    $sql = "SELECT * FROM property_contact WHERE property_id=".intval($id);

    $qr=$db->query_first($sql);

    return $qr;

}

function getAllFavouriteProperty(){
    global $db,$CONFIG,$CONFIG_MEMBER_LINK,$params;
    $user_id = intval($_SESSION["login"]["id"]);
    $total = $db->query_first("SELECT COUNT(p.id) as num_row FROM property p LEFT JOIN favourite_property fp ON p.id=fp.pid  WHERE uid=".$user_id." AND lang='".$_SESSION["lang"]."'");

    $page=intval($params[count($params)-1]) ? intval($params[count($params)-1]) :1;
    $page=intval($page);
    if ($page<0) $page=1;
    $totalRows=$total['num_row'];
    $limit = $CONFIG['rows_per_page_manage_property_draft'];
    $total=ceil($totalRows/$limit);
    $start = (($page*$limit)-$limit);


    $sql 	 = "SELECT p.* FROM property p LEFT JOIN favourite_property fp ON p.id=fp.pid  WHERE uid=$user_id AND lang='".$_SESSION["lang"]."' ORDER BY fp.id DESC LIMIT $start,$limit";


    $property = $db->query($sql);

    return array("property"=>$property,"page"=>$page,"total"=>$total);

}

function get_all_property_page($mode="posted",$condition=""){

    global $db,$CONFIG,$CONFIG_MEMBER_LINK,$params;

    $user_id = intval($_SESSION["login"]["id"]);

    if($mode=="pending"){
        $status = " AND `status` = 'pending'";
    }elseif($mode=="draft"){
        $status = " AND `status` = 'draft'";
    }else{
        $status = " AND `status` = 'actived'";
    }

    $sel_all = $db->query_first("SELECT COUNT(id) as m FROM property WHERE member_id=".$user_id.$status.$condition." AND lang='".$_SESSION["lang"]."'");

    $sql 	 = "SELECT *
                FROM property
                WHERE member_id=".$user_id.$status.$condition." AND lang='".$_SESSION["lang"]."' ORDER BY create_date DESC";

    $page=intval($params[count($params)-1]) ? intval($params[count($params)-1]) :1;
    $page=intval($page);
    if ($page<0) $page=1;
    $number=$sel_all['m'];
    $limit = $CONFIG['rows_per_page_manage_property_draft'];
    $total=ceil($number/$limit);
    $start = (($page*$limit)-$limit);

    $property = $db->query($sql." LIMIT ".$start.",".$limit);
    //Xu ly ban ghi cuoi cung thi sap xep lai trang
    $rows = $db->num_rows($property);
    if($page >1 && $rows<1)
    {
        $page = $page -1;

        $total=ceil($number/$limit);
        $start = (($page*$limit)-$limit);

        $property = $db->query($sql." LIMIT ".$start.",".$limit);

        server_redirect(get_link("member"));
    }

    return array("property"=>$property,"page"=>$page,"total"=>$total);

}
function get_all_cat_by_type($type) {
    global $db,$CONFIG;

    $result = $db->query("SELECT cat_id,cat_title FROM cat_property WHERE cat_parent= ".intval($type)." ORDER BY cat_order ASC");

    if($result&&intval($type)>0){
        return $result;
    }

    return false;
}
function get_str_district_id($city_id){

    global $db;

    $sel_c = $db->query("SELECT geo_id FROM geographic WHERE geo_parent = ".intval($city_id));
    $str_id = "(0";
    while($dist_id=$db->fetch_array($sel_c))
    {
        $str_id .=",".$dist_id["geo_id"];
    }

    $str_id.=")";

    return $str_id;
}
function get_all_geography($parent_id=0){

    global $db;

    $result = $db->query("SELECT geo_id,geo_title FROM geographic WHERE geo_parent = ".intval($parent_id));

    if($result){
        return $result;
    }

    return false;
}
?>